package org.beasy.charts
{
	import mx.charts.AxisRenderer;
	import mx.charts.LinearAxis;
	import mx.charts.chartClasses.IAxisRenderer;
	import mx.formatters.NumberFormatter;
	
	/**
	 * 是否显示千位符
	 * 
	 * @default false
	 */
	[Style(name="useThousandsSeparator", type="Boolean", inherit="no")]
	
	/**
	 * 保留小数点位数
	 * 
	 * @default 0
	 */	
	[Style(name="precision", type="int", format="Length", inherit="no")]
	
	/**
	 * 前缀
	 * 
	 * @param null
	 */	
	[Style(name="prefix", type="String", inherit="no")]
	
	/**
	 * 后缀
	 * 
	 *@default null 
	 */	
	[Style(name="suffix", type="String", inherit="no")]
	
	/**
	 * 货币单位
	 * 
	 * @param value
	 */	
	[Style(name="moneyUnit", type="com.beasy.charts.MoneyUnits", inherit="no")]
	
	/**
	 * 扩展过的AxisRendererPlus
	 * 支持定义标签格式:
	 * 1,支持千们符
	 * 2,小数点保留位数
	 * 3,前缀
	 * 4,后缀
	 * 5,大数字单位缩写
	 * 
	 * @author Marco
	 */	
	public class AxisRendererPlus extends AxisRenderer
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function AxisRendererPlus()
		{
			super();
			labelFunction = myLabelFunction;
			numberFmt = new NumberFormatter();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		/**
		 * @private
		 */		
		private var numberFmt:NumberFormatter;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * @private
		 * 
		 * 根据设定的样式值在labelFunction中转换显示的标签.
		 */		
		public function myLabelFunction(axisRenderer:IAxisRenderer, label:String):String
		{
			var useThousandsSeparator:Boolean = getStyle("useThousandsSeparator");
			var precision:int = getStyle("precision");
			var prefix:String = getStyle("prefix");
			var suffix:String = getStyle("suffix");
			var moneyUnit:MoneyUnits = getStyle("moneyUnit");
			
			if( axisRenderer.axis is LinearAxis )
			{
				var num:Number = Number( label );
				if( moneyUnit )
					num = MoneyUnits.formatByUnit(moneyUnit.unit, num);
				
				numberFmt.precision = precision;
				numberFmt.useThousandsSeparator = useThousandsSeparator;
				label = numberFmt.format(num);
				if( prefix )
					label = prefix+label;
				if( moneyUnit && moneyUnit.unit != MoneyUnits.NONE )
					label += moneyUnit.label;
				if( suffix )
					label += suffix;
			}
			return label;
		}
		
		
		
	}
}